﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Windows.Media.Animation;
using System.IO;
using System.Net;
using System.Net.Sockets;


namespace MYServer
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Storyboard std = null; //演示图板
        public MainWindow()
        {
            InitializeComponent();

            // 从资源中把Key为std的Storyboard读出来
            std = this.Resources["std"] as Storyboard;
            // 声明用于监听连接请求的Socket
            Socket Server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            IPEndPoint local = new IPEndPoint(IPAddress.Any, 1377); //监听所有网络接口上的地址
            Server.Bind(local);// 绑定本地终结点
            Server.Listen(100);// 侦听连接请求
            // 开始异步接受传入的连接请求
            Server.BeginAccept(new AsyncCallback(this.AcceptSocketCallback), Server);
        }

        /// <summary>
        /// 接受传入的Socket的回调
        /// </summary>
        private void AcceptSocketCallback(IAsyncResult ia)
        {
            Socket _socket = ia.AsyncState as Socket;
            Socket accptSocket = _socket.EndAccept(ia);
            try
            {
                IPEndPoint remote = (IPEndPoint)accptSocket.RemoteEndPoint;
                // 显示客户端的IP
                Dispatcher.BeginInvoke(new Action<string>(this.SetIPForText), remote.Address.ToString());
                StateObject so = new StateObject();
                so.theSocket = accptSocket;
                // 开始异步接收消息
                accptSocket.BeginReceive(so.Buffer, 0, so.Buffer.Length, SocketFlags.None, new AsyncCallback(this.ReceiveCallback), so);
            }
            catch
            {

            }
            // 继续接受连接请求
            _socket.BeginAccept(new AsyncCallback(this.AcceptSocketCallback), _socket);
        }
        /// <summary>
        /// 接收消息的回调
        /// </summary>
        private void ReceiveCallback(IAsyncResult ia)
        {
            StateObject _so = ia.AsyncState as StateObject;
            Socket _socket = _so.theSocket;
            try
            {
                int n = _socket.EndReceive(ia);//n就是接收到的字节数
                string msg = Encoding.UTF8.GetString(_so.Buffer, 0, n);
                // 判断客户端发送了啥命令
                switch (msg)
                {
                    case "play":
                        Dispatcher.BeginInvoke(new Action(this.Play), null);
                        break;
                    case "pause":
                        Dispatcher.BeginInvoke(new Action(this.Pause), null);
                        break;
                    case "stop":
                        Dispatcher.BeginInvoke(new Action(this.Stop), null);
                        break;
                    default:
                        break;
                }
            }
            catch
            {
            }
            _so = new StateObject();
            _so.theSocket = _socket;
            // 继续接收消息
            _socket.BeginReceive(_so.Buffer,
                                0,
                                _so.Buffer.Length,
                                SocketFlags.None,
                                new AsyncCallback(this.ReceiveCallback),
                                _so);
        }
        /// <summary>
        /// 显示客户端的IP
        /// </summary>
        private void SetIPForText(string ip)
        {
            this.txtDisplay.Text = "客户端IP：" + ip;
        }

        #region 控制动画的方法
        private void Play()
        {
            std.Begin();
        }
        private void Pause()
        {
            std.Pause();
        }
        private void Stop()
        {
            std.Stop();
        }
        #endregion
    }

    /// <summary>
    /// 用于异步Socket操作传递的状态对象
    /// </summary>
    public class StateObject
    {
        private const int BUFFER_SIZE = 512;

        public byte[] Buffer { get; set; }

        public Socket theSocket { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        public StateObject()
        {
            this.Buffer = new byte[BUFFER_SIZE];
        }
    }
}
